<%--
  Created by IntelliJ IDEA.
  User: 24284
  Date: 2023/3/20
  Time: 10:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title>后台布局</title>
    <link rel="stylesheet" media="all" href="${pageContext.request.contextPath}/static/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/static/layui/layui.js"></script>
    <script src="${pageContext.request.contextPath}/static/echarts/echarts.js"></script>
    <script src="${pageContext.request.contextPath}/static/js/html2canvas.js"></script>
    <script src="${pageContext.request.contextPath}/static/js/jspdf.debug.js"></script>
    <script src="${pageContext.request.contextPath}/static/js/jq.js"></script>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
</head>
<body>
<!--添加功能的form-->
<form class="layui-form" action="" id="insertDeptForm"
      style="display: none;margin-right: 40px;margin-top: 20px">
    <div class="layui-form-item">
        <label class="layui-form-label">部门名称</label>
        <div class="layui-input-block">
            <input id="deptName" type="text" name="deptName" autocomplete="off" placeholder="请输入部门名称"
                   class="layui-input"><span style="color: red"></span>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">排序</label>
        <div class="layui-input-block">
            <input type="number" name="orderNum" autocomplete="off" placeholder="请输入排序"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="checkbox" checked="" name="status" lay-skin="switch" lay-filter="switchStatus"
                   lay-text="可用|不可用">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit id="insertDeptButton" lay-filter="insertDeptFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<!--编辑功能的form-->
<form class="layui-form" action="" id="updateDeptForm" lay-filter="updateDeptForm"
      style="display: none;margin-right: 40px;margin-top: 20px">
    <input hidden name="updateBy" th:value="${session.username}">
    <input id="editDeptId" type="hidden" name="deptId">
    <div class="layui-form-item">
        <label class="layui-form-label">部门名称</label>
        <div class="layui-input-block">
            <input id="editDeptName" type="text" name="deptName" autocomplete="off" placeholder="请输入部门名称"
                   class="layui-input"><span style="color: red"></span>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">排序</label>
        <div class="layui-input-block">
            <input type="number" name="orderNum" autocomplete="off" placeholder="请输入排序"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="checkbox" id="status" name="status" lay-skin="switch" lay-filter="switchStatus"
                   lay-text="可用|失效">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button id="updateDeptBtn" class="layui-btn" lay-submit lay-filter="updateDeptFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<!--顶部搜索框-->
<form class="layui-form" action="" style="margin-top: 10px">
    <div class="layui-inline">
        <label class="layui-form-label">部门名称:</label>
        <div class="layui-input-inline">
            <input type="text" name="deptName" autocomplete="off" placeholder="部门名称" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">创建人:</label>
        <div class="layui-input-inline">
            <input type="text" name="createBy" autocomplete="off" placeholder="创建人" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">修改人:</label>
        <div class="layui-input-inline">
            <input type="text" name="updateBy" autocomplete="off" placeholder="修改人" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-input-inline">
            <button class="layui-btn" lay-submit lay-filter="searchFilter">搜索</button>
            <button type="reset" class="layui-btn layui-btn-primary" id="myButton">重置</button>
        </div>
    </div>
</form>
<table id="showDept" lay-filter="deptFilter"></table>
<script src="layui/layui.js" charset="UTF-8"></script>
</body>
<!--顶部工具栏-->
<script type="text/html" id="deptToolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-xs" lay-event="addDept">
            <i class="layui-icon layui-icon-add-1"></i>添加
        </button>
        <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="deleteDepts">
            <i class="layui-icon layui-icon-delete"></i>删除
        </button>
    </div>
</script>
<!--实现部门管理操作功能按钮-->
<script type="text/html" id="deptBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!--状态按钮-->
<script type="text/html" id="deptStatus">
    {{# if (d.status === '0'){}}
    <button class="layui-btn layui-btn-xs" lay-event="state">正常</button>
    {{# }else {}}
    <button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="state">失效</button>
    {{# }}}
</script>
<script>
    layui.use(['table', 'layer', 'jquery', 'form'], function () {
        let table = layui.table;
        let layer = layui.layer;
        let $ = layui.jquery;
        let form = layui.form;

        let deptTable = table.render({
            elem: '#showDept'
            , height: 550
            , url: '${pageContext.request.contextPath}/dept/pagination' //数据接口
            , toolbar: '#deptToolbar' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter', 'exports', 'print']
            , title: 'deptTable'
            , page: true //开启分页
            , cols: [
                [ //表头
                    {type: 'checkbox', fixed: 'left'}
                    , {field: 'deptId', title: '编号', width: 80, sort: true, fixed: true}
                    , {field: 'deptName', title: '部门名称', width: 100}
                    , {field: 'orderNum', title: '排序', width: 80, sort: true}
                    , {field: 'status', title: '状态', width: 80, toolbar: "#deptStatus"}
                    , {field: 'createBy', title: '创建人', width: 80, sort: true}
                    , {field: 'createTime', title: '创建时间', width: 200, sort: true}
                    , {field: 'updateBy', title: '修改人', width: 100}
                    , {field: 'updateTime', title: '修改时间', width: 200, sort: true}
                    , {fixed: 'right', align: 'center', title: '操作', toolbar: '#deptBar', width: 120}
                ]
            ]
        });
        //监听行工具事件
        table.on('tool(deptFilter)', function (obj) {
            let data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    //删除数据库中的数据
                    $.ajax({
                        url: "${pageContext.request.contextPath}/dept/delete",
                        type: "POST",
                        dataType: "JSON",
                        data: {idList: obj.data.deptId},
                        success: function (ret) {
                            if (ret.code == 0) {
                                //刷新表格数据
                                deptTable.reload({
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                });
                                layer.msg(ret.msg, {icon: 6});
                            } else {
                                layer.msg(ret.msg, {icon: 5});
                            }
                        }
                    });
                    layer.close(index);
                    //向服务端发送删除指令
                });
            } else if (layEvent === 'edit') {
                if (data.status == 0) {
                    document.getElementById("status").checked = true;
                } else {
                    document.getElementById("status").checked = false;
                }
                form.val('updateDeptForm', {
                    'deptId': data.deptId,
                    'deptName': data.deptName,
                    'orderNum': data.orderNum,
                });
                layer.open({
                    title: '编辑部门',
                    type: 1,
                    area: ['500px', '500px'],
                    content: $("#updateDeptForm")//这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                });
            }
        });

        //鼠标离开输入框判断是否重名
        $("#deptName").blur(function () {
            $("#deptName").next().html("");
            $('#insertDeptButton').removeClass("layui-btn-disabled").attr("disabled", false);
            if ($("#deptName").val()!==null&&$("#deptName").val()!==""){
                $.ajax({
                    url: "${pageContext.request.contextPath}/dept/findDeptName.do",
                    data: {deptName: $("#deptName").val()},
                    dataType: "json",
                    type: "get",
                    success: function (re) {
                        console.log($("#deptName").val())
                        if (re.code == 0) {
                            $('#insertDeptButton').removeClass("layui-btn-disabled").attr("disabled", false);
                            // return re.msg
                            // $("#addSub").prop("class","layui-btn")
                        } else {
                            $("#deptName").next().html(re.msg);
                            $('#insertDeptButton').addClass("layui-btn-disabled").attr("disabled", true);
                        }
                    }
                })
            }
        })



        //鼠标离开输入框判断是否重名
        $("#editDeptName").blur(function () {
            $("#editDeptName").next().html("");
            $('#updateDeptBtn').removeClass("layui-btn-disabled").attr("disabled", false);
            if ($("#editDeptName").val()!==null&&$("#editDeptName").val()!==""){
                $.ajax({
                    url: "${pageContext.request.contextPath}/dept/findDeptName.do",
                    data: {deptName: $("#editDeptName").val(),
                        deptId:$("#editDeptId").val()
                    },
                    dataType: "json",
                    type: "get",
                    success: function (re) {
                        console.log($("#editDeptName").val())
                        if (re.code == 0) {
                            $('#updateDeptBtn').removeClass("layui-btn-disabled").attr("disabled", false);
                            // return re.msg
                            // $("#addSub").prop("class","layui-btn")
                        } else {
                            $("#editDeptName").next().html(re.msg);
                            $('#updateDeptBtn').addClass("layui-btn-disabled").attr("disabled", true);
                        }
                    }
                })
            }
        })

        //form表单实现更新功能
        form.on('submit(updateDeptFilter)', function (data) {
            if (data.field.status === 'on') {
                data.field.status = 0;
            } else {
                data.field.status = 1;
            }
            //提交表单数据
            $.ajax({
                url: "${pageContext.request.contextPath}/dept/update",
                type: "POST",
                dataType: "JSON",
                data: data.field,
                success: function (ret) {
                    if (ret.code == 0) {
                        //关闭对话框
                        layer.closeAll();
                        //刷新表格数据
                        deptTable.reload({
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        });
                        layer.msg(ret.msg, {icon: 6});
                    } else {
                        layer.msg(ret.msg, {icon: 5});
                    }
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //监听头工具栏事件
        table.on('toolbar(deptFilter)', function (obj) {
            let checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                case 'addDept':
                    document.getElementById("insertDeptForm").reset();
                    layer.open({
                        title: '添加部门',
                        type: 1,
                        area: ['500px', '450px'],
                        content: $("#insertDeptForm")
                    });
                    break;
                case 'deleteDepts':
                    if (data.length == 0) {
                        layer.msg('请选择一行');
                    } else {
                        layer.confirm('真的删除行么', function () {
                            //for循环收集所有选中的id
                            let ids = [];
                            for (let i = 0; i < data.length; i++) {
                                ids[i] = data[i].deptId;
                            }
                            console.log(ids+"ids11111")
                            $.ajax({
                                url: "${pageContext.request.contextPath}/dept/delete",
                                type: "POST",
                                dataType: "JSON",
                                data: {idList: ids.join(",")},
                                success: function (ret) {
                                    if (ret.code == 0) {
                                        //刷新表格数据
                                        deptTable.reload({
                                            page: {
                                                curr: 1 //重新从第 1 页开始
                                            }
                                        });
                                        layer.msg(ret.msg, {icon: 6});
                                    } else {
                                        layer.msg(ret.msg, {icon: 5});
                                    }
                                }
                            });
                        });
                    }
                    break;
            }
        });

        //form表单实现添加功能
        form.on('submit(insertDeptFilter)', function (data) {
            if (data.field.status === 'on') {
                data.field.status = 0;
            } else {
                data.field.status = 1;
            }
            //提交表单数据
            if (!isNaN(data.field.orderNum)) {
                $.ajax({
                    url: "${pageContext.request.contextPath}/dept/insert",
                    type: "POST",
                    dataType: "JSON",
                    data:data.field,
                    //data: JSON.stringify(data.field),
                   // contentType:'application/json;charset=UTF-8',
                    success: function (ret) {
                        if (ret.code == 0) {
                            //关闭对话框
                            layer.closeAll();
                            //刷新表格数据
                            deptTable.reload({
                                page: {
                                    curr: 1 //重新从第 1 页开始
                                }
                            });
                            layer.msg(ret.msg, {icon: 6});
                        } else {
                            layer.msg(ret.msg, {icon: 5});
                        }
                    }
                });
            } else {
                layer.msg("排序只能是数字！")
            }

            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //实现顶部搜索框
        form.on('submit(searchFilter)', function (data) {
            //执行重载
            deptTable.reload({
                url: "http://localhost:8080/team/dept/findIf",
                where: {
                    deptName: data.field.deptName,
                    createBy: data.field.createBy,
                    updateBy: data.field.updateBy
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        //重置条件查询表单，刷新table数据
        $("#myButton").click(function () {
            deptTable.reload({
                url:'http://localhost:8080/team/dept/pagination',
                where: { //设定异步数据接口的额外参数，任意设
                    deptName: ""
                    , createBy: ""
                    , updateBy: ""
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });
    });
</script>
</html>

